package com.hz.sdka.sdka;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import com.anythink.core.api.ATAdInfo;
import com.anythink.nativead.api.ATNative;
import com.anythink.nativead.api.ATNativeAdView;
import com.anythink.nativead.api.ATNativeDislikeListener;
import com.anythink.nativead.api.ATNativeEventExListener;
import com.anythink.nativead.api.ATNativePrepareInfo;
import com.anythink.nativead.api.NativeAd;
import com.hz.sdka.commona.SDKContext;
import com.hz.sdka.commona.api.feed.HZFeedAdView;
import com.hz.sdka.commona.api.feed.OnHZFeedListener;
import com.hz.sdka.commona.bean.HZAdInfo;
import com.hz.sdka.commona.utils.LogUtils;

public class AdNativea implements HZFeedAdView {

    private final HZAdInfo hzAdInfo;
    private final ATNative atNative;
    private String TAG = "NativeAda[feed ad] ===>";
    ATNativeAdView atNativeAdView;
    OnHZFeedListener listener;
    ATNativePrepareInfo mNativePrepareInfo;
    private NativeAd nativeAd;

    public AdNativea(ATNative atNative, ATNativeAdView view, HZAdInfo hzAdInfo) {
        this.atNativeAdView = view;
        this.hzAdInfo = hzAdInfo;
        this.atNative = atNative;
    }


    @Override
    public View getFeedView() {
        return atNativeAdView;
    }

    @Override
    public void setListener(OnHZFeedListener listener) {
        this.listener = listener;
    }


    private void getNativeView(String scenario) {
        nativeAd = atNative.getNativeAd(scenario);
        if (nativeAd != null) {
            nativeAd.setDislikeCallbackListener(new ATNativeDislikeListener() {
                @Override
                public void onAdCloseButtonClick(ATNativeAdView atNativeAdView, ATAdInfo atAdInfo) {
                    if (listener != null) {
                        listener.onFeedClosed();
                    }
                    LogUtils.d(TAG, "[onAdCloseButtonClick] ");
                }
            });
            nativeAd.setNativeEventListener(new ATNativeEventExListener() {
                @Override
                public void onDeeplinkCallback(ATNativeAdView atNativeAdView, ATAdInfo atAdInfo, boolean b) {
                    LogUtils.d(TAG, "[onDeeplinkCallback] ");
                }

                @Override
                public void onAdImpressed(ATNativeAdView atNativeAdView, ATAdInfo atAdInfo) {
                    LogUtils.d(TAG, "[onAdImpressed] ");
                    hzAdInfo.setEcpm(String.valueOf(atAdInfo.getEcpm()));
                    hzAdInfo.setNetworkPlacementId(atAdInfo.getNetworkPlacementId());
                    hzAdInfo.setNetworkType(atAdInfo.getAdNetworkType());
                    hzAdInfo.setShowId(atAdInfo.getShowId());
                    if (listener != null) {
                        listener.onFeedShown(hzAdInfo);
                    }
                }

                @Override
                public void onAdClicked(ATNativeAdView atNativeAdView, ATAdInfo atAdInfo) {
                    LogUtils.d(TAG, "[onAdClick] ");
                    if (listener != null) {
                        listener.onFeedClicked();
                    }
                }

                @Override
                public void onAdVideoStart(ATNativeAdView atNativeAdView) {
                    LogUtils.d(TAG, "[onAdVideoStart] ");
                }

                @Override
                public void onAdVideoEnd(ATNativeAdView atNativeAdView) {
                    LogUtils.d(TAG, "[onAdVideoEnd] ");
                }

                @Override
                public void onAdVideoProgress(ATNativeAdView atNativeAdView, int i) {
                    LogUtils.d(TAG, "[onAdVideoProgress] ");
                }
            });
        }
    }


    @Override
    public void render(int layoutId, int adViewWidth, String scenario) {
        getNativeView(scenario);
        if (nativeAd != null) {
            mNativePrepareInfo = null;
            try {
                int height = adViewWidth * 600 / 1024;
                height = height <= 0 ? FrameLayout.LayoutParams.WRAP_CONTENT : height;
                if (nativeAd.isNativeExpress()) {
                    nativeAd.renderAdContainer(atNativeAdView, null);
                } else {
                    View selfRenderView = LayoutInflater.from(SDKContext.getInstance().getContext()).inflate(layoutId, null);
                    mNativePrepareInfo = new ATNativePrepareInfo();
                    SelfRenderViewUtila.bindSelfRenderView(SDKContext.getInstance().getContext(), nativeAd.getAdMaterial(), selfRenderView, mNativePrepareInfo, height);
                    nativeAd.renderAdContainer(atNativeAdView, selfRenderView);
                }
                if (mNativePrepareInfo == null) {
                    nativeAd.prepare(atNativeAdView);
                } else nativeAd.prepare(atNativeAdView, mNativePrepareInfo);
                atNativeAdView.setVisibility(View.VISIBLE);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }

    @Override
    public void destory() {
        if (nativeAd != null) {
            nativeAd.destory();
        }
    }

}
